home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue50 / IPC / Anonymous Pipes / API / ChildMainFormUnit.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-28  |  1.5 KB  |  72 lines

  1. unit ChildMainFormUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Edit1: TEdit;
  12.     Button1: TButton;
  13.     procedure FormCreate(Sender: TObject);
  14.     procedure FormDestroy(Sender: TObject);
  15.     procedure Button1Click(Sender: TObject);
  16.   private
  17.     PipeWrite: THandle;
  18.   end;
  19.  
  20. {$ifdef Ver90}
  21.   //This exception class did not exist in Delphi 2
  22.   EWin32Error = class(Exception);
  23. {$endif}
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.DFM}
  31.  
  32. {$ifdef Ver90}
  33. //This function class did not exist in Delphi 2
  34. function Win32Check(RetVal: Bool): Bool;
  35. var
  36.   LastError: DWord;
  37. begin
  38.   Result := RetVal;
  39.   if not RetVal then
  40.   begin
  41.     LastError := GetLastError;
  42.     if LastError <> Error_Success then
  43.       raise EWin32Error.CreateFmt( 'Win32 Error.  Code: %d.'#10'%s',
  44.         [LastError, SysErrorMessage(LastError)])
  45.     else
  46.       raise EWin32Error.Create('A Win32 API function failed')
  47.   end;
  48. end;
  49. {$endif}
  50.  
  51. procedure TForm1.FormCreate(Sender: TObject);
  52. begin
  53.   PipeWrite := GetStdHandle(Std_Output_Handle)
  54. end;
  55.  
  56. procedure TForm1.FormDestroy(Sender: TObject);
  57. begin
  58.   CloseHandle(PipeWrite);
  59. end;
  60.  
  61. procedure TForm1.Button1Click(Sender: TObject);
  62. var
  63.   BytesWritten: DWord;
  64.   Msg: String;
  65. begin
  66.   Msg := Edit1.Text + #13#10;
  67.   Win32Check(WriteFile(PipeWrite, Msg[1], Length(Msg),
  68.                    BytesWritten, nil));
  69. end;
  70.  
  71. end.
  72.